<?php
require_once 'interface.php';
class mProduction
{
    use \control\interfaces;

    public string $date;
    public string $time;
    /**
     * 执行
     */

    public function __construct()
    {
        // 启用扩展
        $this->init();
        $this->date = date('Y-m-d');
        $this->time = date('Y-m-d H:i:s');
    }

    private function head($title = ""): string
    {
        $root = root;
        $version = version;
        $css = <<<EOT
	<link href="{$root}static/stylesheet/animate.min.css?v=$version" rel="stylesheet">
		<link href="{$root}static/stylesheet/m.css?v=$version" rel="stylesheet">
EOT;
        $js = <<<EOT
	<script src="{$root}static/javascript/js.js?v=$version"></script>
EOT;
        $meta = "
	<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no\">
	<meta name='apple-mobile-web-app-capable' content='yes'>
	<meta name='apple-mobile-web-app-status-bar-style' content='black'>
	<meta name='format-detection' content='telephone=no,email=no,adress=no'>
		";
        if (empty($title)) {
            $title = para("title");
        }
        $keywords = para("keywords");
        $description = para("description");
        return <<<EOT
<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta charset="UTF-8">
	<meta name="keywords" content="$keywords">
	<meta name="description" content="$description">
	<title>$title</title>
$meta $css
	<link href="{$root}favicon.ico?v=$version" rel="Bookmark">
	<link href="{$root}favicon.ico?v=$version" rel="icon">
	<link href="{$root}favicon.ico?v=$version" rel="shortcut icon">
	<link href="{$root}favicon.ico?v=$version" rel="apple-touch-icon">
	<script src="{$root}static/javascript/jquery-1.11.2.min.js?v=$version"></script>
	<script src="{$root}static/javascript/lib.js?v=$version"></script>
	<script src="{$root}static/plugins/laydate/laydate.js?v=$version"></script>
$js
	<script>
	let root = '$root';
	</script>
</head>
<body>

EOT;
    }

    /*底部*/
    private function footer(): string
    {
        return <<<eof
<script>

function commitForm(form, url){
    //防止高频点击
    let finger = 2;
    if (finger === 2) {
        //串联表单
        var formData = new FormData();
        formData.append('file', $('#fileUpload')[0].files[0]);
        $("form").find(":input").each(function() {
            var name = $(this).attr("name");
            var value = $(this).val();
            if(name !== 'file'){
                formData.append(name, value);
            }
        });        
        //异步提交数据
        $.ajax({
            type: 'POST',
            url: url, // 替换为你的服务器端点
            data: formData,
            contentType: false, // 不设置内容类型，由 FormData 自己确定
            processData: false, // 不处理发送的数据，因为数据是 FormData 对象，不需要转换
            success: function(data) {
                console.log()
                var item = jQuery.parseJSON(data);

                if (item.code==1) {
                    location.href = root+'control/mSuccess.html';
                } else {
                    msg(item.warn, 'error');
                }
            },
            error: function(xhr, status, error) {
                console.error('Submission failed:', error);
            }
        });
        // $.post(url, serialize, function (data) {
        //     //console.log(data);
        //     if (data.code==1) {
        //         location.href = root+'control/mSuccess.html';
        //     } else {
        //         msg(data.warn, 'error');
        //     }
        // }, "json");
    }
}
function msg(text, type = 'info') {
    return $.Toast(text, type, {
        fullscreen: false,
        timeout: 2000,
        progress: true,
        positionClass: "toast-top-right",
    });
}
</script>

</body>
		</html>
eof;

    }

    /*返回*/
    public function indexHtml(): string
    {
        $html = $this->contentHtml();
        //返回
        return $this->head() . $html .$this->footer();
    }

    private function contentHtml(): string
    {
        $downIco = root.'img/control/down_arrow.svg';
        $type = select(["name" => "type", 'title' => '类型', 'option' => ['提送货','监督检查','承包商'], 'class' => 'not-arrow text-input', "value" => '', "change"=>"typeSelect(this)"]);
        $role_type1 = select(["name" => "role_type1", 'title' => '提货角色', 'option' => ['驾驶员','押运员'], 'class' => 'not-arrow text-input', "value" => '']);
        $role_type2 = select(["name" => "role_type2", 'title' => '承包角色', 'option' => ['承包负责人','承包作业人'], 'class' => 'not-arrow text-input', "value" => '', "change"=>"typeSelect1(this)"]);
        $role_type2_1 = select(["name" => "role_type2_1", 'title' => '承包作业人角色', 'option' => ['辅工','电工证','焊工证','高处作业证','叉车证','动火作业'], 'class' => 'not-arrow text-input', "value" => '']);
        $borrowInfo = select(["name" => "borrowInfo", 'title' => '借用信息', 'option' => ['安全帽','定位卡','劳保鞋','工作服'], 'class' => 'not-arrow text-input', "value" => '','other' => "multiple style='height: 100px;'"]);//
        $shortName = select(["name" => "shortName", 'title' => '车牌', 'option' => explode('，', para('shortNameOption')), "value" => '','class' => 'not-arrow text-input shortNameSelect',]) . text(["name" => "carNo", 'title' => '车牌号','class' => 'text-input', "value" => '']);
        $leaveTimeTime = text(array('name'=>'leaveTime','laydate'=>['type'=>'datetime'],'other'=>"readOnly",'class'=>'text-input','title'=>'请选择'));
        $arrivalTimeTime = text(array('name'=>'arrivalTime','laydate'=>['type'=>'datetime'],'other'=>"readOnly",'class'=>'text-input','title'=>'请选择'));
        $visitDateTime = text(array('name'=>'visitDate','laydate'=>['type'=>'datetime'],'other'=>"readOnly",'class'=>'text-input','title'=>'请选择'));
        $managerData = find(['table' => "staff",'where' => "state in ('转正','试用','留职')"]);
        $managerData = array_column($managerData['array'],'name','stid');
        $managerId = select(["name" => "managerId", 'title' => '接待人', 'option' => $managerData, 'class' => 'not-arrow text-input', "value" => '']);
        $approach = text(array('name'=>'approach','class'=>'text-input','title'=>'请填写'));
        $leaving = text(array('name'=>'leaving','class'=>'text-input','title'=>'请填写'));
        $idNum = text(array('name'=>'idNum','class'=>'text-input','title'=>'请填写'));
        $leaveUnit = text(array('name'=>'leaveUnit','class'=>'text-input','title'=>'请填写'));
        $name = text(array('name'=>'name','class'=>'text-input','title'=>'请填写'));
        $contactTel = text(array('name'=>'contactTel','class'=>'text-input','title'=>'请填写'));
        $reason = text(array('name'=>'reason','class'=>'text-input','title'=>'请填写'));
//        <div class="row-li">
//                <div class="row-li-title">上次安全告知日期</div>
//                <div class="row-li-value">$visitDateTime</div>
//            </div>
        return <<<eof
<div class="body-content">
      <!-- 状态栏 -->
      <div class="topbar">
        <div class="topbar-text">来访登记</div>
      </div>
      <div class="content-parent">
     
      <div style="height: 44px"></div>
         <form name="visitorForm" id="visitorForm">
            <div class="row-li">
                <div class="row-li-title">类型</div>
                <div class="row-li-value">$type<img class="row-li-icon" src="$downIco"></div>
            </div>
            <div class="row-li" style="display: none;" id="role_type1">
                <div class="row-li-title">提货角色</div>
                <div class="row-li-value">$role_type1<img class="row-li-icon" src="$downIco"></div>
            </div>
            <div class="row-li" style="display: none;" id="role_type2">
                <div class="row-li-title">承包角色</div>
                <div class="row-li-value">$role_type2<img class="row-li-icon" src="$downIco"></div>
            </div>
            <div class="row-li" style="display: none;" id="role_type2_1">
                <div class="row-li-title">承包作业人角色</div>
                <div class="row-li-value">$role_type2_1<img class="row-li-icon" src="$downIco"></div>
            </div>
            <!-- 车牌号必填 -->
            <div class="row-li">
                <div class="row-li-title">进场装载</div>
                <div class="row-li-value">$approach</div>
            </div>
            <div class="row-li">
                <div class="row-li-title">离场装载</div>
                <div class="row-li-value">$leaving</div>
            </div>
            <div class="row-li">
                <div class="row-li-title">证件号码</div>
                <div class="row-li-value">$idNum</div>
            </div>
            <div class="row-li" style="height: 100px;">
                <div class="row-li-title">借用信息</div>
                <div class="row-li-value">$borrowInfo<img class="row-li-icon" src="$downIco"></div>
            </div>
            <div class="row-li">
                <div class="row-li-title">来访单位</div>
                <div class="row-li-value">$leaveUnit</div>
            </div>
            <div class="row-li">
                <div class="row-li-title">姓名</div>
                <div class="row-li-value">$name</div>
            </div>
            <div class="row-li">
                <div class="row-li-title">联系电话</div>
                <div class="row-li-value">$contactTel</div>
            </div>
 
            <div class="row-li">
                <div class="row-li-title">车牌号</div>
                <div class="row-li-value" style="display: flex;">$shortName</div>
            </div>
            <div class="row-li">
                <div class="row-li-title">进场时间</div>
                <div class="row-li-value">$arrivalTimeTime</div>
            </div>
            <div class="row-li">
                <div class="row-li-title">出场时间</div>
                <div class="row-li-value">$leaveTimeTime</div>
            </div>
            
            <div class="row-li">
                <div class="row-li-title">接待人</div>
                <div class="row-li-value">$managerId</div>
            </div>
            <div class="row-li">
                <div class="row-li-title">来访事由</div>
                <div class="row-li-value">$reason</div>
            </div>
            <div class="row-li">
                <div class="row-li-title">附件</div>
                <div class="row-li-value">
                   <input type="file" id="fileUpload" name="file">
                </div>
            </div>
         </form>
      </div>
      <div class="text-button" onclick="commitForm('visitorForm',root+'interface/v1/index.php?cla=visitApp&fun=appCommitEdit')">确认提交</div>
    </div>
<script>
function typeSelect(that){
    let name = $(that).val();
    if(name == "提送货"){
        $("#role_type1").show();
        $("#role_type2").hide();
        $("#role_type2_1").hide();
    }
    if(name == "承包商"){
        $("#role_type2").show();
        $("#role_type2_1").show();
        $("#role_type1").hide();
    }
    if(name == "监督检查"){
        $("#role_type1").hide();
        $("#role_type2").hide();
        $("#role_type2_1").hide();
    }
}
function typeSelect1(that) {
      let name = $(that).val();
      if(name == "承包作业人"){
          $("#role_type2_1").show();
      }else {
          $("#role_type2_1").hide();
      }
}
</script>
<style>
.shortNameSelect {
 width: 30% !important;
}
</style>
eof;
    }

}

//实例化对象
$mVisitorClass = new mProduction();
//打印结果
echo $mVisitorClass->indexHtml();

